﻿import mx.utils.Delegate;

class Ship{
	private var __instance:MovieClip;

	private var __dirRotation:Number = 0;
	private var __maxRotation:Number = 20;
	private var __speedRotation:Number = 4;
	private var __vRotation:Number = 0;

	private var __isMove:Boolean = false;
	private var __maxMove:Number = 20;
	private var __vMove:Number = 0;
	private var __aMove:Number = 2;
	
	private var __gMove:Number;

	function Ship(instance_mc:MovieClip){
		__instance = instance_mc;
		
		init();
	}
	
	private function init():Void{
		G = .5;
		
		__instance.lineStyle(0, 0x000000, 100);
		__instance.moveTo(10, -2);
		__instance.lineTo(10, 2);
		__instance.lineTo(0, 5);
		__instance.lineTo(0, -5);
		__instance.lineTo(10, -2);
		
		var fire_mc:MovieClip = __instance.createEmptyMovieClip("fire_mc", 2);
		fire_mc.lineStyle(0, 0x000000, 100);
		fire_mc.moveTo(-2, 0);
		fire_mc.lineTo(-5, 0);
		fire_mc.moveTo(-2, -2);
		fire_mc.lineTo(-5, -3);
		fire_mc.moveTo(-2, 2);
		fire_mc.lineTo(-5, 3);
		fire_mc._visible = false;
		
		__instance._rotation = -90;
		__instance.onEnterFrame = Delegate.create(this, shipOnEnterFrame);
	}
	
	public function rotate(dir:Number):Void{
		__dirRotation = dir;
	}
	
	public function move(isMove:Boolean):Void{
		__isMove = isMove;
		
		__instance.fire_mc._visible = __isMove;
	}
	
	public function get G():Number{return __gMove;}
	public function set G(g:Number):Void{__gMove=g;}
	
	private function shipOnEnterFrame():Void{
		//------------------------------------------------rotation
		var aRotation:Number = __speedRotation * __dirRotation;
		__vRotation += aRotation;
		if(Math.abs(__vRotation)>__maxRotation)__vRotation = __maxRotation * (__vRotation<0?-1:1);
		if(__vRotation>0)__vRotation--;
		if(__vRotation<0)__vRotation++;

		__instance._rotation += __vRotation;

		if(__instance._rotation>-90)__instance._rotation--;
		if(__instance._rotation<-90)__instance._rotation++;

		//------------------------------------------------move
		if(__isMove){
			__vMove += __aMove;
		}
		if(__vMove>__maxMove)__vMove = __maxMove;
		if(__vMove>0)__vMove--;
		if(__vMove<0)__vMove=0;
		
		var vx:Number = Math.cos(__instance._rotation * Math.PI / 180) * __vMove;
		var vy:Number = Math.sin(__instance._rotation * Math.PI / 180) * __vMove;
		vy += __gMove;
		
		__instance._x += vx;
		__instance._y += vy;
	}
	
	public function getRotation():Number{
		return __instance._rotation + 90;
	}
	
	public function getMovingSpeed():Number{
		return __vMove;
	}
	
}